﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections;

namespace KALE_Scanning
{
    public partial class LookupForm : Form
    {
        private AGC_WebServices.Control[] _oField;
        private readonly string _subSiteUrl;
        private int _counter = 0;
        private Hashtable _lookupValues = new Hashtable();
        List<Control> _controlList = new List<Control>();
        readonly AGC_WebServices.Control _fieldForControl;
        private readonly string _mainSiteUrl = "";
        public LookupForm(AGC_WebServices.Control fieldForControl, string mainSiteUrl, string subSiteUrl)
        {
            InitializeComponent();
            _mainSiteUrl = mainSiteUrl;
            _fieldForControl = fieldForControl;
            _subSiteUrl = subSiteUrl;
            GetField();
            ShowAllField();
        }
        public void GetField()
        {
            using (AGC_WebServices.Service ws = new AGC_WebServices.Service())
            {
                //ws.UseDefaultCredentials = true;
                ws.Credentials = UserProperty.UserProperties.Credential;
                Guid listId = new Guid(_fieldForControl.LookupListId);
                Guid webId = _fieldForControl.LookupWebID;
                _oField = ws.GetLibraryFields(_mainSiteUrl, webId, listId);
            }
        }

        public void ShowAllField()
        {
            string listId = _fieldForControl.LookupListId;
            foreach (AGC_WebServices.Control item in _oField)
            {
                Methods.Site = _mainSiteUrl;
                AGC_WebServices.LookupFieldValue[] lookupBirim = null;
                Control ctrl = Methods.CreateDynamicControl(item, ref  LookupPanel, _mainSiteUrl, ref _counter, out lookupBirim, listId);
                _lookupValues.Add(ctrl.Name, lookupBirim);
                _controlList.Add(ctrl);
            }
        }

        private void Button1Click(object sender, EventArgs e)
        {
            List<System.Windows.Forms.Control> list = new List<System.Windows.Forms.Control>();
            System.Windows.Forms.Control control;
            System.Windows.Forms.Control controlNext;
            System.Windows.Forms.Control controlBase = LookupPanel.Controls[0];
            for (int index = 0; index < LookupPanel.Controls.Count; index++)
            {
                if (index + 2 > LookupPanel.Controls.Count)
                {
                    control = LookupPanel.Controls[index];
                    controlNext = controlBase;
                }
                else
                {
                    control = LookupPanel.Controls[index];
                    controlNext = LookupPanel.Controls[index + 1];
                }

                control.Focus();
                controlNext.Focus();


                if (!control.CausesValidation)
                {
                    list.Add(control);
                }
            }

            if (list.Count > 0)
            {
                DialogResult = DialogResult.None;
                MessageBox.Show("Lütfen Gerekli Alanları Doldurunuz!");
                list[0].Focus();
                list.Clear();
            }
            else
            {
                List<Sharepoint_Copy_WS.FieldInformation> abc = new List<Sharepoint_Copy_WS.FieldInformation>();
                List<AGC_WebServices.FieldInformation> fieldInformationList = new List<AGC_WebServices.FieldInformation>();
                abc = Methods.CreateDynamicCopyField(_oField, ref _lookupValues, ref _controlList);
                List<string> typeList = new List<string>();
               
                foreach (Sharepoint_Copy_WS.FieldInformation item in abc)
                {
                    AGC_WebServices.FieldInformation agcFieldInformation = new AGC_WebServices.FieldInformation();
                    agcFieldInformation.DisplayName = item.DisplayName;
                    agcFieldInformation.Id = item.Id;
                    agcFieldInformation.InternalName = item.InternalName;
                    //agcFieldInformation.Type = item.Type.ToString();
                    agcFieldInformation.Value = item.Value;
                    typeList.Add(item.Type.ToString());
                    fieldInformationList.Add(agcFieldInformation);
                }

                using (AGC_WebServices.Service ws = new AGC_WebServices.Service())
                {
                    //ws.UseDefaultCredentials = true;
                    ws.Credentials = UserProperty.UserProperties.Credential;
                    Guid listId = new Guid(_fieldForControl.LookupListId);
                    ws.InsertLookupField(_mainSiteUrl, listId, fieldInformationList.ToArray(), typeList.ToArray());
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }
    }
}
